package filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EncodeFilter implements javax.servlet.Filter{
	Logger log = LoggerFactory.getLogger(this.getClass());
	FilterConfig config;

	@Override
	public void destroy() {
		log.info("================================");
		log.info("Encode filter destroy");
		log.info("================================");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse reponse,
			FilterChain chain) throws IOException, ServletException {
		String encoding = config.getInitParameter("encode");
		log.info("================================");
		log.info("encoding:" + encoding);
		log.info("================================");
		request.setCharacterEncoding(encoding);
		reponse.setCharacterEncoding(encoding);
		chain.doFilter(request, reponse);
		
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		this.config = config;
		log.info("================================");
		log.info("Encode filter init.");
		log.info("================================");
	}	
}
